/*
 * The MIT License
 * 
 * Copyright (c) 2008 Comcast Interactive Media
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cim.sf.core.command.equinox;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.osgi.framework.console.CommandProvider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

import cim.sf.core.command.CommandShell;

/**
 * 
 * @author <a href="mailto:jeremy_volkman@cable.comcast.com">Jeremy Volkman</a>
 *
 */
public class Activator implements BundleActivator {

    private ServiceTracker equinoxCommandTracker = null;
    ServiceTracker shellTracker;
    private Map<CommandProvider, EquinoxCommandRegistrar> equinoxRegistrarMap;
    BundleContext context;

	public void start(BundleContext context) throws Exception {
	    this.context = context;
        equinoxRegistrarMap = new HashMap<CommandProvider, EquinoxCommandRegistrar>();

        this.shellTracker = new ServiceTracker(context, CommandShell.class.getName(), null);
	    this.shellTracker.open();
        
	    this.equinoxCommandTracker = new ServiceTracker(context,
                CommandProvider.class.getName(), new EquinoxCommandTracker());
        this.equinoxCommandTracker.open();
        
        context.registerService(CommandProvider.class.getName(), new LegacyCommandProvider(this), null);
	}

	public void stop(BundleContext context) throws Exception {
	    if (this.equinoxCommandTracker != null) {
            this.equinoxCommandTracker.close();
            this.equinoxCommandTracker = null;
        }
	    if (this.shellTracker != null) {
	        this.shellTracker.close();
	        this.shellTracker = null;
	    }
	    equinoxRegistrarMap = null;
        this.context = null;
	}
    
    class EquinoxCommandTracker implements ServiceTrackerCustomizer {

        public synchronized Object addingService(ServiceReference ref) {
            BundleContext ctx = ref.getBundle().getBundleContext();
            if (ctx == null) {
                ctx = context;
            }
            CommandProvider provider = (CommandProvider) ctx.getService(ref);
            if (provider == null) {
                return null;
            }
            EquinoxCommandRegistrar registrar = new EquinoxCommandRegistrar(
                    provider);
            registrar.open(ctx);
            if (equinoxRegistrarMap != null) {
                equinoxRegistrarMap.put(provider, registrar);
            }
            return provider;
        }

        public void modifiedService(ServiceReference ref, Object obj) {
            // Do nothing
        }

        public synchronized void removedService(ServiceReference ref, Object obj) {
            if (equinoxRegistrarMap == null) return;
            EquinoxCommandRegistrar registrar = equinoxRegistrarMap.remove(obj);
            if (registrar != null) {
                registrar.close();
            }
        }

    }
}
